define([
    'jquery',
    "jqzoomCore",
    "signin",
    "jquery-cookie"
], function ($, jqzoomCore, signin) {

    //加载商品详情
    function goodsDownload() {
        let goodsId = signin.valueByName(location.search, 'goodsId');
        let baseUrl = signin.getBaseUrl();

        $.get(baseUrl + "/goods/detail", {
                goodsId: goodsId
            },
            function (res) {

                $('.summary>h3>strong').text(res.data.goodsName);
                $('.goodsinfo li:eq(0)').html('<span>商品编号：</span>' + res.data.goodsSn);
                $('.market_price>em').html(res.data.marketPrice);
                $('.shop_price>strong').html(res.data.shopPrice);
                $('.goodsinfo li:eq(3)').html('<span>上架事件：</span>' + res.data.createTime);
                // 放大镜效果显示
                $('.midpic>a').prop('href', res.data.goodsImg[0].goodsImg);
                $('.midpic img').prop('src', res.data.goodsImg[0].goodsImg);

                let smallPic = '';

                res.data.goodsImg.forEach(function (item, index) {
                    smallPic += `<li class="${index == 0 ?'cur':''} ">
                <a class="${index == 0 ?'zoomThumbActive':''}" href="javascript:void(0);" rel="{gallery: 'gal1', smallimage: '${item.goodsImg}',largeimage: '${item.goodsImg}'}"><img src="${item.goodsImg}"></a>
            </li>`;
                });

                $(".smallpic .smallpic_wrap ul").html(smallPic);
            });

    }

    //商品详情图点击切换/放大镜
    function goodsImgTab() {

        //点击切换图片 当前小图加上红色边框
        $(".smallpic .smallpic_wrap").on("click", "  ul ", function () {

            $(this).addClass("zoomThumbActive").parent("li").siblings().children("a").removeClass("zoomThumbActive");

            return false;
        });



        //左右标签点击切换
        let iNow = $(".smallpic .smallpic_wrap li a").index(); //设置当前显示图片的下标
        let aImgs = 4; //图片数量
        // console.log(aImgs);
        let aBtns = $(".smallpic #backward,#forward"); //左右点击

        //鼠标移入--放大镜效果
        $(".summary .preview ").on("mouseenter", " .midpic a", function () {
            // console.log(1);
            $(function () {
                $('.jqzoom').jqzoom({
                    zoomType: 'standard',
                    lens: true,
                    preloadImages: false,
                    alwaysOn: false,
                    title: false,
                    zoomWidth: 200,
                    zoomHeight: 200
                });
            });
        });

        //商品轮播
        function tab() {
            iNow == 0 ? aBtns.eq(0).removeClass("on").addClass("off") : $(".smallpic #backward").addClass("on").removeClass("off");
            iNow == aImgs ? aBtns.eq(1).removeClass("on").addClass("off") : $(".smallpic #forward").addClass("on").removeClass("off");
            let iTarget = iNow == aImgs ? iNow * -62 + 248 : iNow * -62;
            $(".smallpic .smallpic_wrap ul").css("left", iTarget + "px");
        }

        // 点击切换
        aBtns.click(function () {
            if ($(this).index() == 0) {
                iNow--;
                iNow = Math.max(0, iNow); //最大值
                // console.log(iNow);
            } else {
                iNow++;
                iNow = Math.min(aImgs, iNow); //最小值
                // console.log(iNow);
            }
            tab();
            $(".smallpic .smallpic_wrap").find("li a").eq(iNow).addClass("zoomThumbActive").parent("li").siblings().children("a").removeClass("zoomThumbActive");

        });



    }

    //评论切换
    function detailTab() {
        $(".detail .detail_hd").on("mouseenter", "li", function () {
            // console.log($(this));
            $(this).addClass("on").siblings().removeClass("on");
            $(".detail_bd div").eq($(this).index()).toggleClass("none").siblings().removeClass("none");
        });
    }

    //加入购物车
    function addGoods() {
        let userInfo = null;
        let goodsId = signin.valueByName(location.search, 'goodsId'); //商品Id
        signin.checkUserInfo() == null ? '' : userInfo = JSON.parse(signin.checkUserInfo());
        let baseUrl = signin.getBaseUrl();

        // 加减点击事件
        $(".choose ul").on("click", "#reduce_num,#add_num", function () {
            let inputVal = $("input[name=amount]").val();
            // console.log(inputVal);
            if (this.id == "reduce_num") {
                console.log(123)
                inputVal == 1 ? alert("数量已经为1，不能再减少！") : inputVal--;
            } else {
                inputVal++;
            }
            $("input[name=amount]").val(inputVal);
            return false;
        });

        // 加入购物车,存入cookie
        $(".choose ul").on("click", ".add_btn", function () {

            let inputVal = parseInt($("input[name=amount]").val());
            if (userInfo) {
                $.post(baseUrl + "/cart/add", {
                        userId: userInfo.id,
                        goodsId: goodsId,
                        goodsCount: inputVal
                    },
                    function (res) {
                        if (res != 0) {
                            alert(res.msg);
                        }
                        console.log(res);
                    });
            } else {

                let first = $.cookie("goods") == null ? true : false; //商品是否加过购物车
                if (first) {
                    //存到cookie
                    let cookieStr = `[{"goodsId":${goodsId},"num":${inputVal}}]`;
                    $.cookie("goods", cookieStr, {
                        expires: 7
                    });
                } else {
                    let same = false; //假设没有添加过
                    let cookieStr = $.cookie("goods"); //取出cookie中存的商品信息
                    console.log(cookieStr);
                    let cookieArr = JSON.parse(cookieStr);
                    console.log(cookieArr)
                    for (let i = 0; i < cookieArr.length; i++) {
                        if (cookieArr[i].goodsId == goodsId) {
                            cookieArr[i].num += inputVal;
                            same = true;
                            break;
                        }
                    }
                    if (!same) {
                        //如果没有添加过，新增商品数据
                        let obj = {
                            goodsId: goodsId,
                            num: parseInt(inputVal)
                        };
                        cookieArr.push(obj);
                    }
                    //最后，存回cookie中
                    $.cookie("goods", JSON.stringify(cookieArr), {
                        expires: 7
                    });
                }
            }
            alert("成功加入购物车");
            return false;
        });
    }



    return {

        goodsDownload: goodsDownload,
        goodsImgTab: goodsImgTab,
        detailTab: detailTab,
        addGoods: addGoods
    }


});